﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CreatureSoft.Hoot.Service.DocumentManagent;
using CreatureSoft.Hoot.Data.Repositories;
using Rhino.Mocks;
using CreatureSoft.Hoot.Data.Infrastructure;
using CreatureSoft.Hoot.Domain.DocumentManagent;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CreatureSoft.Hoot.Test.MSTest.DocumentManagement
{
    [TestClass]
    public class FolderTests
    {
        [TestMethod]
        public void CanSaveAndLoadFolder()
        {
            string folderName = "Test Folder";
            string folderDescription = "Test Folder Description";           

            var factory = new DatabaseFactory();
            var folderRepository = new FolderRepository(factory);
            var unitOfWork = new UnitOfWork(factory);

            var newFolder = new Folder { Name = folderName, Description = folderDescription };
            var serivce = new FolderService(folderRepository, unitOfWork);

            serivce.CreateFolder(newFolder);
            
            var folder = serivce.GetFolder(newFolder.Id);

            Assert.AreEqual(folderName, folder.Name);
            Assert.AreEqual(folderDescription, folder.Description);
            Assert.IsTrue(folder.Id > 0, "Id is not greater than 0.");            
        }

        [TestMethod]
        public void CanSaveAndLoadFolderWithSubFolders()
        {
            string folderName = "Parent Test Folder";
            string folderDescription = "Parent Test Folder Description";

            var factory = new DatabaseFactory();
            var folderRepository = new FolderRepository(factory);
            var unitOfWork = new UnitOfWork(factory);

            var newFolder = new Folder { Name = folderName, Description = folderDescription };
            newFolder.SubFolders.Add(new Folder { Name = folderName + "1", Description = folderDescription + "1" });
            newFolder.SubFolders.Add(new Folder { Name = folderName + "2", Description = folderDescription + "2" });
            newFolder.SubFolders.Add(new Folder { Name = folderName + "3", Description = folderDescription + "3" });

            var serivce = new FolderService(folderRepository, unitOfWork);

            serivce.CreateFolder(newFolder);


             factory = new DatabaseFactory();
             folderRepository = new FolderRepository(factory);
             unitOfWork = new UnitOfWork(factory);
             serivce = new FolderService(folderRepository, unitOfWork);

            var folder = serivce.GetFolder(newFolder.Id);

            Assert.AreEqual(folderName, folder.Name);
            Assert.AreEqual(folderDescription, folder.Description);
            Assert.IsTrue(folder.Id > 0, "Id is not greater than 0.");

            var counter = 1;
            foreach (var subFolder in folder.SubFolders)
            {
                Assert.AreEqual(folderName + counter.ToString(), subFolder.Name);
                Assert.AreEqual(folderDescription + counter.ToString(), subFolder.Description);
                Assert.IsTrue(subFolder.Id > 0, "Id is not greater than 0.");
                counter++;
            }
        }

        //[TestMethod]
        //public void CanLoadFolderWithSubFolders()
        //{
        //    string folderName = "Parent Test Folder";
        //    string folderDescription = "Parent Test Folder Description";

        //    var factory = new DatabaseFactory();
        //    var folderRepository = new FolderRepository(factory);
        //    var unitOfWork = new UnitOfWork(factory);

            
        //    var serivce = new FolderService(folderRepository, unitOfWork);

        //    var folder = serivce.GetFolder(51);

        //    Assert.AreEqual(folderName, folder.Name);
        //    Assert.AreEqual(folderDescription, folder.Description);
        //    Assert.IsTrue(folder.Id > 0, "Id is not greater than 0.");

        //    var counter = 1;
        //    foreach (var subFolder in folder.SubFolders)
        //    {
        //        Assert.AreEqual(folderName + counter.ToString(), subFolder.Name);
        //        Assert.AreEqual(folderDescription + counter.ToString(), subFolder.Description);
        //        Assert.IsTrue(subFolder.Id > 0, "Id is not greater than 0.");
        //        counter++;
        //    }
        //}
    }
}
